<template>
  <div>
    <el-dialog :title="info.isAdd ?'角色添加':'角色修改'" :visible.sync="info.isshow" @close="close">
      user:{{user}}
      <el-form :model="user">
        <el-form-item label="角色名称" label-width="120px">
          <el-input v-model="user.rolename" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="角色权限" label-width="120px">
          <!-- 树形组件 -->
          <!-- data：展示的数据
           props：children判断是否有子节点，label页面展示的字段
           @node-click 点了节点，可以获取到点击的节点的数据       
          -->
          <el-tree
            :data="treeData"
            :props="{children: 'children',label: 'title'}"
            show-checkbox
            node-key="id"
            ref="tree"
          ></el-tree>
        </el-form-item>

        <el-form-item label="状态" label-width="120px">
          <el-switch v-model="user.status" :active-value="1" :inactive-value="2"></el-switch>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel">取 消</el-button>
        <el-button type="primary" v-if="info.isAdd" @click="add()">添加</el-button>
        <el-button type="primary" v-else @click="update">修改</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import { mapGetters, mapActions } from "vuex";
import { IndexRoutes } from "../../../router/index";
import { successAlert } from "../../../utils/alert.js";
// import {reqmenuadd} from "../../../http/http.js"
import {
  reqroleadd,
  reqmenuedit,
  reqmenuinfo,
  reqrolelist,
  reqmenulist,
  reqroleinfo,
  reqroleedit
} from "../../../http/api";
import { formClose } from "../../../mixins";

export default {
  //接受info
  props: ["info", "arr"],
  mixins: [formClose],
  data() {
    return {
      treeData: [],
      //初始化表单数据
      user: {
        rolename: "",
        menus: "",
        status: 1
      },
      //路由集合
      IndexRoutes
    };
  },
  computed: {
    ...mapGetters({
      userInfo: "userInfo"
    })
  },
  mounted() {
    //请求树的数据
    reqmenulist({ istree: true }).then(res => {
      if (res.data.code == 200) {
        this.treeData = res.data.list ? res.data.list : [];
      }
    });
  },
  methods: {
    ...mapActions({
      changeUserInfo: "changeUserInfo"
    }),

    //清空user
    empty() {
      this.user = {
        rolename: "",
        menus: "",
        status: 1
      };
      //清空树的数据
      this.$refs.tree.setCheckedKeys([]);
    },
    //点了添加
    add() {
      //先获取树的数据，并赋值给user.menus
      this.user.menus = JSON.stringify(this.$refs.tree.getCheckedKeys());
      reqroleadd(this.user).then(res => {
        if (res.data.code == 200) {
          //弹成功
          successAlert(res.data.msg);
          //弹框清空
          this.empty();
          //弹框消失
          this.info.isshow = false;
          //重新请求列表
          this.$emit("init");
        }
      });
    },
    //上级菜单发生了改变
    changepid() {
      //如果上级菜单是顶级菜单，那么类型就是目录。否则就是菜单，看pid
      if (this.user.pid == 0) {
        this.user.type = 1;
      } else {
        this.user.type = 2;
      }
    },
    //获取一条数据
    getOne(id) {
      reqroleinfo({ id: id }).then(res => {
        if (res.data.code == 200) {
          this.user = res.data.list;
          //补id
          this.user.id = id;
          //补树
          this.$refs.tree.setCheckedKeys(JSON.parse(this.user.menus));
        }
      });
    },
    // 修改
    update() {
      //先获取树的数据，并赋值给user.menus
      this.user.menus = JSON.stringify(this.$refs.tree.getCheckedKeys());
      reqroleedit(this.user).then(res => {
        if (res.data.code == 200) {
          //弹成功
          successAlert(res.data.msg);
          if (this.user.id == this.userInfo.roleid) {
            this.changeUserInfo({});
            this.$router.push("/login");
            return;
          }
          //弹框清空
          this.empty();
          //弹框消失
          this.info.isshow = false;
          //重新请求列表
          this.$emit("init");
        }
      });
    }
  }
};
</script>
<style scoped lang="less">
@import "../../../less/index.less";
</style>